/**
 * @file SendQueueWorker.cs
 * @author Zane Mayo
 * @date 19/5/2009
 */

using System;

namespace MxitAPI {
	
	
	public class SendQueueWorker {
		
		private static log4net.ILog log = log4net.LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );
		
		private ProtocolMessageSendQueue protocolMessageSendQueue;
		private MessageTransceiver protocolMessageTransceiver;
		
		public SendQueueWorker( ProtocolMessageSendQueue protocolMessageSendQueue, MessageTransceiver protocolMessageTransceiver ) {
			this.protocolMessageSendQueue = protocolMessageSendQueue;
			this.protocolMessageTransceiver = protocolMessageTransceiver;			
		}
		
	/*	public int test(){
			System.Threading.Thread.Sleep(1000);
			return 1;
		}*/
		
		/**
		 * Run in a seperate thread, this method monitors the message queue for new messages to send and sends them when they exist
		 */
		public void SendProtocolMessages() {
			while( true ) {
				lock( protocolMessageSendQueue ) {
					if ( !protocolMessageSendQueue.IsEmpty ) {
						
						log.Debug( "Message found in queue" );
						ProtocolMessage protocolMessage = protocolMessageSendQueue.GetNextProtocolMessage();
						protocolMessageTransceiver.SendMessage( protocolMessage );
						System.Threading.Thread.Sleep( 1000 );
						/*log.Debug( test() );
						log.Debug( "HELLO" );
						Console.Out.WriteLine( 	test() );
						Console.Out.WriteLine( 	"HELLO" );*/
						
					}
					else {
						//log.Debug( "No message found in queue" );
						System.Threading.Thread.Sleep( 1000 );
					}
				}
			}
		}
		
		
				
	}
}
